#include <stdio.h>
#include <libxml/xmlsave.h>
#include <xmlsec/xmldsig.h>

#include "common.h"

int main() {
	xmlSecKeyPtr key;
	xmlSecKeyInfoCtxPtr ctx;
	xmlDocPtr doc;
	xmlNodePtr root;
	xmlNsPtr ns;
	xmlSaveCtxtPtr save;

	libSetup();
	key = loadRootKey();
	ctx = xmlSecKeyInfoCtxCreate(NULL);
	xmlSecKeyInfoCtxInitialize(ctx, NULL);
	ctx->mode = xmlSecKeyInfoModeWrite;
#if 0
	ctx->keyReq.keyId = xmlSecKeyDataIdUnknown;
	ctx->keyReq.keyUsage = xmlSecKeyDataUsageAny;
#endif
	ctx->keyReq.keyType = xmlSecKeyDataTypePublic;

	doc = xmlNewDoc("1.0");
	root = xmlNewDocNode(doc, NULL, "KeyInfo", NULL);
	ns = xmlNewNs(root, xmlSecDSigNs, "ki");
	xmlSetNs(root, ns);
	xmlDocSetRootElement(doc, root);
	xmlAddChild(root, xmlNewDocNode(doc, ns, "KeyValue", NULL));
	xmlSecKeyInfoNodeWrite(root, key, ctx);
	printf("Content-type: text/xml\n\n");
	fflush(stdout);
	save = xmlSaveToFd(1, "UTF-8", XML_SAVE_NO_EMPTY|XML_SAVE_NO_DECL);
	xmlSaveDoc(save, doc);
	xmlSaveClose(save);
	return 0;
}
